import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
// import user from './modules/user'
import getters from './getters'


Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    app,
    // user
  },
  state: {
    token: localStorage.getItem("token") === undefined ? null : localStorage.getItem("token"),
    userInfo: JSON.parse(sessionStorage.getItem("userInfo") === undefined ? null : sessionStorage.getItem("userInfo")),
  },
  mutations:{
    //set方法
    //添加token信息
    SET_TOKEN: (state,token) => {
      state.token = token
      localStorage.setItem("token",token)
    },
    //在session中添加用户信息
    SET_USERINFO: (state,userInfo) => {
      state.userInfo = userInfo
      sessionStorage.setItem("userInfo",JSON.stringify(userInfo))
    },
    //退出登录时清空用户信息
    REMOVE_INFO: (state) => {
      state.token = null
      state.userInfo = null
      localStorage.setItem("token",null)
      sessionStorage.setItem("userInfo",null)
    }
  },
  getters,
  actions:{

    // Logout({ state }){
    //   return new Promise((resolve, reject) => {
    //     this.$axios.get('/manage/login/out',{
    //       headers: {
    //         "Authorization": localStorage.getItem("token")
    //       }
    //     }).then(() => {
    //       this.REMOVE_INFO()
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

  }
})
